namespace DotNetNuke.UI.WebControls
{
    using DotNetNuke.UI.Utilities;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    internal class DNNMenuUpLevelWriter : WebControl, IDNNMenuWriter
    {
        private DNNMenu m_objMenu;

        protected override void RenderChildren(HtmlTextWriter writer)
        {
            IEnumerator enumerator=null;
            try
            {
                enumerator = this.m_objMenu.MenuNodes.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    ((MenuNode) enumerator.Current).Render(writer);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    ((IDisposable) enumerator).Dispose();
                }
            }
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
            writer.AddAttribute(HtmlTextWriterAttribute.Class, this.m_objMenu.CssClass);
            writer.AddAttribute(HtmlTextWriterAttribute.Name, this.m_objMenu.UniqueID);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.m_objMenu.UniqueID.Replace(":", "_"));
            writer.AddAttribute("orient", ((int) this.m_objMenu.Orientation).ToString());
            writer.AddAttribute("sysimgpath", this.m_objMenu.SystemImagesPath);
            if (Strings.Len(this.m_objMenu.Target) > 0)
            {
                writer.AddAttribute("target", this.m_objMenu.Target);
            }
            if (this.m_objMenu.ImageList.Count > 0)
            {
                string expression = "";
                foreach (NodeImage image in this.m_objMenu.ImageList)
                {
                    expression = expression + StringType.FromObject(Interaction.IIf(Strings.Len(expression) > 0, ",", "")) + image.ImageUrl;
                }
                writer.AddAttribute("imagelist", expression);
            }
            writer.AddAttribute("rarrowimg", this.m_objMenu.RootArrowImage);
            writer.AddAttribute("carrowimg", this.m_objMenu.ChildArrowImage);
            if (Strings.Len(this.m_objMenu.WorkImage) > 0)
            {
                writer.AddAttribute("workimg", this.m_objMenu.WorkImage);
            }
            if (Strings.Len(this.m_objMenu.DefaultNodeCssClass) > 0)
            {
                writer.AddAttribute("css", this.m_objMenu.DefaultNodeCssClass);
            }
            if (Strings.Len(this.m_objMenu.DefaultChildNodeCssClass) > 0)
            {
                writer.AddAttribute("csschild", this.m_objMenu.DefaultChildNodeCssClass);
            }
            if (Strings.Len(this.m_objMenu.DefaultNodeCssClassOver) > 0)
            {
                writer.AddAttribute("csshover", this.m_objMenu.DefaultNodeCssClassOver);
            }
            if (Strings.Len(this.m_objMenu.DefaultNodeCssClassSelected) > 0)
            {
                writer.AddAttribute("csssel", this.m_objMenu.DefaultNodeCssClassSelected);
            }
            if (Strings.Len(this.m_objMenu.MenuBarCssClass) > 0)
            {
                writer.AddAttribute("mbcss", this.m_objMenu.MenuBarCssClass);
            }
            if (Strings.Len(this.m_objMenu.MenuCssClass) > 0)
            {
                writer.AddAttribute("mcss", this.m_objMenu.MenuCssClass);
            }
            if (Strings.Len(this.m_objMenu.DefaultIconCssClass) > 0)
            {
                writer.AddAttribute("cssicon", this.m_objMenu.DefaultIconCssClass);
            }
            if (Strings.Len(this.m_objMenu.JSFunction) > 0)
            {
                writer.AddAttribute("js", this.m_objMenu.JSFunction);
            }
            writer.AddAttribute("postback", ClientAPI.GetPostBackEventReference(this.m_objMenu, "[NODEID]" + ClientAPI.COLUMN_DELIMITER + "Click"));
            if (this.m_objMenu.PopulateNodesFromClient)
            {
                if (ClientAPI.BrowserSupportsFunctionality(ClientAPI.ClientFunctionality.XMLHTTP))
                {
                    writer.AddAttribute("callback", ClientAPI.GetCallbackEventReference(this.m_objMenu, "'[NODEXML]'", "this.callBackSuccess", "oMNode", "this.callBackFail", "this.callBackStatus"));
                }
                else
                {
                    writer.AddAttribute("callback", ClientAPI.GetPostBackClientHyperlink(this.m_objMenu, "[NODEID]" + ClientAPI.COLUMN_DELIMITER + "OnDemand"));
                }
                if (Strings.Len(this.m_objMenu.CallbackStatusFunction) > 0)
                {
                    writer.AddAttribute("callbackSF", this.m_objMenu.CallbackStatusFunction);
                }
            }
            if (Strings.Len(this.m_objMenu.JSFunction) > 0)
            {
                writer.AddAttribute("js", this.m_objMenu.JSFunction);
            }
            writer.RenderBeginTag(HtmlTextWriterTag.P);
            writer.RenderEndTag();
        }

        public void RenderMenu(HtmlTextWriter writer, DNNMenu Menu)
        {
            this.m_objMenu = Menu;
            this.RenderControl(writer);
        }
    }
}

